<?php

require("../config.php");

$rez_init = mysql_fetch_row(mysql_query("SELECT `id`,`alianta` FROM `users` WHERE `user` = '".$user."' LIMIT 1"));

if(isset($_GET["tip"]))
	{ $tip = addentities($_GET["tip"]); } else { $tip = "utilizatori"; }

if(isset($_GET["id"]))
	{ 
	 $id = addentities($_GET["id"]);
	}
	 else
	{
	 $id = $rez_init[0];
	};
	
 if($id == "") { $id = $rez_init[0]; }
 
echo '<table border="0" width="500" height="350" cellpadding="0" cellspacing="0">
       <tr><td style="background-image:url(\'img/tab6.gif\'); background-repeat:no-repeat;" height="30">
		<a href="javascript:void(0)" onMouseUp="javascript:returnare_statistici(\'utilizatori\', \''.$rez_init[0].'\');" onMouseOver="javascript:animatie_but(\'but_statistici_utilizatori\', 1,\'buton\')" onMouseOut="javascript:animatie_but(\'but_statistici_utilizatori\', 0,\'buton\')" onMouseDown="javascript:animatie_but(\'but_statistici_utilizatori\', 2,\'buton\')"><div id="but_statistici_utilizatori" class="butoane_1" style="margin-left:20px;float:left">Utilizatori</div></a>
	    <a href="javascript:void(0)" onMouseUp="javascript:returnare_statistici(\'aliante\', \''.$rez_init[1].'\');" onMouseOver="javascript:animatie_but(\'but_statistici_aliante\', 1,\'buton\')" onMouseOut="javascript:animatie_but(\'but_statistici_aliante\', 0,\'buton\')" onMouseDown="javascript:animatie_but(\'but_statistici_aliante\', 2,\'buton\')"><div id="but_statistici_aliante" class="butoane_1" style="margin-left:3px;float:left">Aliante</div></a>
	   </td></tr>
	   <tr><td valign="top">';

////////////////////////////////////
//////////////Statistici utilizatori
////////////////////////////////////
if(($tip == "utilizatori")||($tip == "user")||($tip == "rang"))
 	{

	//// Cati utilizatori sunt in baza de date?
$nr_users = mysql_num_rows(mysql_query("SELECT `id` FROM `users`"));

	//// Daca cautarea se efectueaza dupa utilizator
if($tip == "user")
	{
	 $query = mysql_query("SELECT `id` FROM `users` WHERE user = '".$id."' LIMIT 1");
	 if(mysql_num_rows($query)>0)
	 		{
			 $rez_id = mysql_fetch_row($query);
	 		 $id = $rez_id[0];
			}
			 else
		 	{ $id = $rez_init[0];}
	}

	//// Daca cautarea se efectueaza dupa rang
if($tip == "rang")
	{
	 if($id>=$nr_users) { $id = $nr_users - 1; } else { $id = $id - 1; }
	 $query = mysql_query("SELECT `id` FROM `users` ORDER BY `populatie` DESC LIMIT ".$id.", 1");
	 if($rez_id = mysql_fetch_array($query)) { $id = $rez_id[0]; } else { $id = $rez_init[0]; } 
	}	

	 echo '<form name="statistici_form" id="statistici_form" action="#" onSubmit="javascript:return false;"><table width="500" cellpadding="0" cellspacing="0">
	 	 	 <tr style="border: 1px #FFFFFF solid; font-family: verdana; font-size: 12; font-weight: bolder; color: #FFFFFF">
			   <td background="img/tab5.gif" height="25" width="50" style="border-left: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">Nr.</td>
			   <td background="img/tab5.gif" height="25" width="190" style="border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">Nume</td>
			   <td background="img/tab5.gif" height="25" width="70" style="border-top: 1px #000000 solid; border-bottom: 1px #000000 solid"><center>Populatie</center></td>
			   <td background="img/tab5.gif" height="25" width="190" style="border-right: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid"><center>Alianta</center></td>
			 </tr>';
/////

	//// Verifica pe ce pozitie este cel din declarat
for($i=1;$i<=$nr_users;$i++)
	{
	 $x = $i-1;
	 $rez = mysql_fetch_row(mysql_query("SELECT `id`,`user` FROM `users` ORDER BY `populatie` DESC LIMIT ".$x.",1"));
	 if($rez[0] == $id) { $user_sel = $rez[1]; break; }
	}

	//// Daca este pe pozitie mai mare de 10 declara pozitia de pe care incepe tabelul statistic
if($i-10>=1) $k = $i - 9;

	//// Daca este pe pozitie mai mare de 10 afisaza incepand cu 10 locuri mai sus
if($i-10>=1)
	{
	 for($k=$k-1;$k<$i;$k++)
	 	{
		 $rez_user = mysql_fetch_row(mysql_query("SELECT `user`,`populatie`,`alianta`,`id` FROM `users` ORDER BY `populatie` DESC LIMIT ".$k.",1"));
	 	 $nr_ord = $k + 1;
		 if($rez_user[3] == $id) $bg = 'bgcolor="gray"'; else $bg = 'background="img/dot.png"';
		 if($rez_user[0] == $user) $bg = 'background="img/tab5.gif"';
		 echo '<tr>
			     <td '.$bg.' style="border-left: 1px #000000 solid"><font color="#34ff47">'.$nr_ord.'&nbsp;</font></td>
			     <td '.$bg.'><a href="javascript:void(0)" onMouseUp="javascript:optiuni_utilizator(\''.$rez_user[3].'\');" >'.$rez_user[0].'&nbsp;</a></td>
			     <td '.$bg.'><center><font color="#34ff47">'.$rez_user[1].'&nbsp;</center></font></td>
			     <td '.$bg.' style="border-right: 1px #000000 solid"><center><a href="javascript:void(0)" onMouseUp="javascript:returnare_statistici(\'aliante\', \''.$rez_user[2].'\');" >'.$rez_user[2].'&nbsp;</a></center></td>
			   </tr>';
		}
	 $incepere = $i-9;
	 $sfarsit = $i;
	}
	else
	//// Daca este pe pozitie mai mica de 10 afisaza incepand cu locul 1 pana la 10
	{
	 for($k=0;$k<=9;$k++)
	 	{
		 $rez_user = mysql_fetch_row(mysql_query("SELECT `user`,`populatie`,`alianta`,`id` FROM `users` ORDER BY `populatie` DESC LIMIT ".$k.",1"));
	 	 $nr_ord = $k + 1;		 
		 if($rez_user[3] == $id) $bg = 'bgcolor="gray"'; else $bg = 'background="img/dot.png"';
		 if($rez_user[0] == $user) $bg = 'background="img/tab5.gif"';
		 echo '<tr>
			     <td '.$bg.' style="border-left: 1px #000000 solid"><font color="#34ff47">'.$nr_ord.'&nbsp;&nbsp;</font></td>
			     <td '.$bg.'><a href="javascript:void(0)" onMouseUp="javascript:optiuni_utilizator(\''.$rez_user[3].'\');">'.$rez_user[0].'&nbsp;</a></td>
			     <td '.$bg.'><center><font color="#34ff47">'.$rez_user[1].'&nbsp;</center></font></td>
			     <td '.$bg.' style="border-right: 1px #000000 solid"><center><a href="javascript:void(0)" onMouseUp="javascript:returnare_statistici(\'aliante\', \''.$rez_user[2].'\');" >'.$rez_user[2].'&nbsp;</a></center></td>
			   </tr>';
		}
	 $incepere = 1;
	 $sfarsit = 10;
	}
	
	//// id-ul user-ului anterior
	$k1 = $i-2; 
	if($k1 >= 0)
		{ $i_ant = mysql_fetch_row(mysql_query("SELECT `id` FROM `users` ORDER BY `populatie` DESC LIMIT ".$k1.",1")); }
		 else
		{ $i_ant = mysql_fetch_row(mysql_query("SELECT `id` FROM `users` ORDER BY `populatie` DESC LIMIT 0, 1")); }
	
	//// id-ul user-ului urmator
	$k2 = $i;
	if ($k2 < $nr_users) 
		{ $i_urm = mysql_fetch_row(mysql_query("SELECT `id` FROM `users` ORDER BY `populatie` DESC LIMIT ".$k2.",1")); }
		 else
		{ $max = $nr_users - 1; $i_urm = mysql_fetch_row(mysql_query("SELECT `id` FROM `users` ORDER BY `populatie` DESC LIMIT ".$max.",1")); }
	
/////			 
	 echo'  <tr style="border: 1px #FFFFFF solid; font-family: verdana; font-size: 12; font-weight: bolder; color: #FFFFFF">
	 		  <td background="img/tab5.gif" height="25" width="50" style="border-left: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">['.$nr_users.']</td>
			  <td background="img/tab5.gif" height="25" width="200" style="border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">
			  <center><a href="javascript:void(0)" onMouseUp="javascript:returnare_statistici(\'utilizatori\', \''.$i_ant[0].'\');" >&lt;</a>&nbsp;'.$incepere.' - '.$sfarsit.'&nbsp;<a href="javascript:void(0)" onMouseUp="javascript:returnare_statistici(\'utilizatori\', \''.$i_urm[0].'\');" >&gt;</a></center>
			  </td>
			  <td background="img/tab5.gif" height="25" width="50" style="border-top: 1px #000000 solid; border-bottom: 1px #000000 solid"><center><input type="text" name="pozitie" size="6" value="'.$i.'" onClick="tratare_statistici(\'pozitie\')"></center></td>
			  <td background="img/tab5.gif" height="25" width="200" style="border-right: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid"><center><input type="text" name="user" size="13" value="'.$user_sel.'" onClick="tratare_statistici(\'user\')">&nbsp&nbsp&nbsp<input type="submit" value=" Cauta " onClick="javascript:statistici_cautare();"></center></td>
			</tr>
		  </table></form>';
	}

////////////////////////////////////
//////////////Statistici aliante
////////////////////////////////////
if($tip == "aliante")
	{
	 echo '<table width="500" cellpadding="0" cellspacing="0">
	 	 	 <tr style="border: 1px #FFFFFF solid; font-family: verdana; font-size: 12; font-weight: bolder; color: #FFFFFF">
			   <td background="img/tab5.gif" height="25" width="50" style="border-left: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">Nr.</td>
			   <td background="img/tab5.gif" height="25" width="190" style="border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">Nume</td>
			   <td background="img/tab5.gif" height="25" width="70" style="border-top: 1px #000000 solid; border-bottom: 1px #000000 solid"><center>Populatie</center></td>
			   <td background="img/tab5.gif" height="25" width="190" style="border-right: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid"><center>Fondator</center></td>
			 </tr>';
/////

$nr_aliante = mysql_num_rows(mysql_query("SELECT * FROM `aliante`"));
if($nr_aliante != 0 ) {
	//// Trece aliantele intr-o matrice
$query = mysql_query("SELECT `nume`,`fondator` FROM `aliante`");
$n = 0;
while($rez_aliante = mysql_fetch_array($query))
	{echo '<Tr><td colspan="4" background="img/dot.png"><font color="#34ff47">';
	 $n++;
	 $mat_aliante[$n][1] = $rez_aliante[0];
	 $populatie = 0;
	 $query2 = mysql_query("SELECT `populatie` FROM `users` WHERE alianta = '".$rez_aliante[0]."'");
	 $mat_aliante[$n][4] = mysql_num_rows($query2);
	 while($rez_user_alianta = mysql_fetch_array($query2))
	 		{ 
			 $populatie = $populatie + $rez_user_alianta[0];
			}
	 $mat_aliante[$n][2] = $populatie;
	 $mat_aliante[$n][3] = $rez_aliante[1];
	 echo '</font></td></tr>';
	}
	
						//// Numele aliantei	$mat_aliante[#][1]
						//// Populatia aliantei	$mat_aliante[#][2]
						//// Fondatorul aliantei	$mat_aliante[#][3]
						//// Numarul de componenti ai aliantei	$mat_aliante[#][4]

	//// Sorteaza aliantele dupa suma numarului de locuitori de la fiecare utilizator
for($i=1;$i<=$nr_aliante;$i++)
	for($j=$i+1;$j<=$nr_aliante;$j++)
		if($mat_aliante[$i][2]<$mat_aliante[$j][2])
			{
			// Interschimba numele aliantelor
			 $aux = $mat_aliante[$i][1];
			 $mat_aliante[$i][1] = $mat_aliante[$j][1];
			 $mat_aliante[$j][1] = $aux;
			// Interschimba populatia aliantelor
			 $aux = $mat_aliante[$i][2];
			 $mat_aliante[$i][2] = $mat_aliante[$j][2];
			 $mat_aliante[$j][2] = $aux;
			// Interschimba fondatorii aliantelor
			 $aux = $mat_aliante[$i][3];
			 $mat_aliante[$i][3] = $mat_aliante[$j][3];
			 $mat_aliante[$j][3] = $aux;
			//Interschimba numarul de utilizatori inscrisi in aliante
			 $aux = $mat_aliante[$i][4];
			 $mat_aliante[$i][4] = $mat_aliante[$j][4];
			 $mat_aliante[$j][4] = $aux;
			}

	//// Afla numarul aliantei care a fost selectata
for($i=1;$i<=$nr_aliante;$i++)
	if($mat_aliante[$i][1] == $id)
		{
		 $alianta_sel = $i;
		 break;		 
		}
		
for($i=1;$i<=$nr_aliante;$i++)
	if($mat_aliante[$i][1] == $rez_init[1])
		{
		 $alianta_user = $i;
		 break;
		}

	//// Afiseaza aliantele
			// Valabil pentru lista cu mai putin de 10 aliante
if(($nr_aliante<=10)&&($nr_aliante>=1))
{
 for($i=1;$i<=$nr_aliante;$i++)
	{
	 if($i == $alianta_sel) { $bg = 'bgcolor="gray"'; } else { $bg = 'background="img/dot.png"'; }
	 if($i == $alianta_user) { $bg = 'background="img/tab5.gif"'; }
	 echo '   <tr>
			     <td '.$bg.' style="border-left: 1px #000000 solid"><font color="#34ff47">'.$i.'&nbsp;</font></td>
			     <td '.$bg.'><font color="#34ff47">'.$mat_aliante[$i][1].'&nbsp;</font></td>
			     <td '.$bg.'><center><font color="#34ff47">'.$mat_aliante[$i][4].' ['.$mat_aliante[$i][2].']&nbsp;</center></font></td>
			     <td '.$bg.' style="border-right: 1px #000000 solid"><font color="#34ff47"><center>'.$mat_aliante[$i][3].'&nbsp;</center></font></td>
			   </tr>';
	}
$incepere = 1;
$sfarsit = $i-1;
} else {
	
	
			// Valabil pentru lista cu mai mult de 10 aliante
	//// Daca este pe pozitie mai mare de 10 declara pozitia de pe care incepe tabelul statistic
if($alianta_sel>=11) $i = $alianta_sel - 9;

	//// Daca este pe pozitie mai mare de 10 afisaza incepand cu 10 locuri mai sus
if($alianta_sel>=11)
	{
	 for($i=$i;$i<=$alianta_sel;$i++)
	 	{
		 if($i == $alianta_sel) { $bg = 'bgcolor="gray"'; } else { $bg = 'background="img/dot.png"'; }
	 	 if($i == $alianta_user) { $bg = 'background="img/tab5.gif"'; }
		 echo '   <tr>
			     <td '.$bg.' style="border-left: 1px #000000 solid"><font color="#34ff47">'.$i.'&nbsp;</font></td>
			     <td '.$bg.'><font color="#34ff47">'.$mat_aliante[$i][1].'&nbsp;</font></td>
			     <td '.$bg.'><center><font color="#34ff47">'.$mat_aliante[$i][4].' ['.$mat_aliante[$i][2].']&nbsp;</center></font></td>
			     <td '.$bg.' style="border-right: 1px #000000 solid"><font color="#34ff47"><center>'.$mat_aliante[$i][3].'&nbsp;</center></font></td>
			   </tr>';
		}
	 $incepere = $alianta_sel-9;
	 $sfarsit = $alianta_sel;
	}
	else
	//// Daca este pe pozitie mai mica de 10 afisaza incepand cu locul 1 pana la 10
	{
	 for($i=1;$i<=10;$i++)
	 	{	 
		 if($i == $alianta_sel) { $bg = 'bgcolor="gray"'; } else { $bg = 'background="img/dot.png"'; }
	 	 if($i == $alianta_user) { $bg = 'background="img/tab5.gif"'; }
	 echo '   <tr>
			     <td '.$bg.' style="border-left: 1px #000000 solid"><font color="#34ff47">'.$i.'&nbsp;</font></td>
			     <td '.$bg.'><font color="#34ff47">'.$mat_aliante[$i][1].'&nbsp;</font></td>
			     <td '.$bg.'><center><font color="#34ff47">'.$mat_aliante[$i][4].' ['.$mat_aliante[$i][2].']&nbsp;</center></font></td>
			     <td '.$bg.' style="border-right: 1px #000000 solid"><font color="#34ff47"><center>'.$mat_aliante[$i][3].'&nbsp;</center></font></td>
			   </tr>';
		}
	 $incepere = 1;
	 $sfarsit = 10;
	}
	}
	
	// Verifica daca alianta selectata este pe pozitia maxima sau pozitia poate fi incrementata
	if($alianta_sel == 1) { $i_ant = $mat_aliante[$alianta_sel][1]; } else { $i_ant = $mat_aliante[$alianta_sel-1][1]; }
	// Verifica daca alianta selectata este pe pozitia minima sau pozitia poate fi micsorata
	if($alianta_sel == $nr_aliante) { $i_urm = $mat_aliante[$alianta_sel][1]; } else { $i_urm = $mat_aliante[$alianta_sel+1][1]; }
	echo'  <tr style="border: 1px #FFFFFF solid; font-family: verdana; font-size: 12; font-weight: bolder; color: #FFFFFF">
	 		  <td background="img/tab5.gif" height="25" width="50" style="border-left: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">['.$nr_aliante.']</td>
			  <td background="img/tab5.gif" height="25" width="200" style="border-top: 1px #000000 solid; border-bottom: 1px #000000 solid"><center><a href="javascript:void(0)" onMouseUp="javascript:returnare_statistici(\'aliante\', \''.$i_ant.'\');" >&lt;</a>&nbsp;'.$incepere.' - '.$sfarsit.'&nbsp;<a href="javascript:void(0)" onMouseUp="javascript:returnare_statistici(\'aliante\', \''.$i_urm.'\');" >&gt;</a><center></td>
			  <td background="img/tab5.gif" height="25" width="50" style="border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">&nbsp;</td>
			  <td background="img/tab5.gif" height="25" width="200" style="border-right: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid">&nbsp;</td>
			</tr>
		  </table>';
	
	} else
	{
	 echo '    <tr>
			     <td background="img/dot.png" style="border-left: 1px #000000 solid" colspan=4><font color="#34ff47"><center>Nici o alianta nu este disponibila!</center></font></td>
			   </tr>';
	 echo'  <tr style="border: 1px #FFFFFF solid; font-family: verdana; font-size: 12; font-weight: bolder; color: #FFFFFF">
	 		  <td background="img/tab5.gif" height="25" width="50" style="border-left: 1px #000000 solid; border-top: 1px #000000 solid; border-bottom: 1px #000000 solid; border-right: 1px #000000 solid" colspan=4>&nbsp;</td>
			</tr>
		  </table>';
	
	}
/////			 
	 
	}
//////////////////////////////////////////////////////////////////////
echo '</td></tr></table>';


?>